package main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import exceptions.NotDirException;

/**
 * File operations
 * 
 * @author gruszek
 * 
 */
public class FileOperations {
	/**
	 * Checks if path is to directory
	 * 
	 * @param path
	 * @return
	 * @throws NotDirException
	 */
	public static File checkDir(String path) throws NotDirException {
		File plik = new File(path);
		if (plik.isDirectory()) {
			return plik;
		} else {
			throw new NotDirException();
		}
	}

	public static void showContent(File show) {
		File[] listOfFiles = show.listFiles();
		for (int i = 0; i < listOfFiles.length; i++)
			System.out.println(listOfFiles[i].getName());
	}

	/**
	 * Creates directories which names are artist Files have to be named (artist
	 * - title.mp3)
	 * 
	 * @param make
	 */
	public static void makeDirs(File make) {
		File[] listOfFiles = make.listFiles();
		for (File listOfFile : listOfFiles) {
			if (listOfFile.isFile()) {
				String name = giveFolderName(listOfFile);
				File directory = new File(name);
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.append(name);
				stringBuilder.append(" ");
				stringBuilder.append(directory.mkdir());
				System.out.println(stringBuilder.toString());
			}
		}
	}

	public static void moveToDirs(File move) {
		File[] listOfFiles = move.listFiles();
		for (File listOfFile : listOfFiles) {
			if (listOfFile.isFile()) {
				String destPath = giveFolderName(listOfFile);
				try {
					FileInputStream input = new FileInputStream(listOfFile);
					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.append(destPath);
					stringBuilder.append("/");
					stringBuilder.append(listOfFile.getName());
					FileOutputStream output = new FileOutputStream(
							stringBuilder.toString());
					byte[] buffer = new byte[1024];
					int read;
					while ((read = input.read(buffer)) != -1) {
						output.write(buffer, 0, read);
					}
					input.close();
					output.close();
					listOfFile.delete();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void unpackFromDirs(File unpack) {
		File[] listOfDirs = unpack.listFiles();
		String absPath = unpack.getAbsolutePath();
		String destPath = new String();
		for (int i = 0; i < listOfDirs.length; i++) {
			if (listOfDirs[i].isDirectory()) {
				File sourceDir = listOfDirs[i].getAbsoluteFile();
				File[] listOfFiles = sourceDir.listFiles();
				for (int j = 0; j < listOfFiles.length
						&& !listOfFiles[j].isDirectory(); j++) {
					String sourcePath = new String(
							listOfFiles[j].getAbsolutePath());
					// System.out.println(sourcePath);
					try {
						destPath = absPath + "/" + listOfFiles[j].getName();
						FileInputStream input = new FileInputStream(sourcePath);
						FileOutputStream output = new FileOutputStream(destPath);
						System.out.println(sourcePath);
						System.out.println(destPath);
						byte[] buffer = new byte[1024];
						int read;
						while ((read = input.read(buffer)) != -1) {
							output.write(buffer, 0, read);
						}
						input.close();
						output.close();
						listOfFiles[j].delete();
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}
		}

	}

	private static String giveFolderName(File file) {
		String name = new String(file.getAbsolutePath().trim());
		name = name.substring(0, (name.length() - 4));
		name = name.split(" - ")[0];
		return name;
	}

}
